using AiQiuQuan.Sport.Model.Enums;
using System.ComponentModel.DataAnnotations;

namespace AiQiuQuan.Sport.Model.Models
{
    /// <summary>
    /// 修改dto
    /// </summary>
    public class ActivityUpdateDto : IValidatableObject
    {
        /// <summary>
        /// 编号
        /// </summary>
        [Required]
        public Guid ID { get; set; }

        /// <summary>
        /// 活动名称
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "活动名称必填")]
        public string Name { get; set; }

        /// <summary>
        /// 省份ID
        /// </summary>
        public Guid? ProvinceId { get; set; }

        /// <summary>
        /// 省名
        /// </summary>
        public string? ProvinceName { get; set; }

        /// <summary>
        /// 城市ID
        /// </summary>
        public Guid? CityId { get; set; }

        /// <summary>
        /// 城市名称
        /// </summary>
        public string? CityName { get; set; }

        /// <summary>
        /// 地点
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "地点必填")]
        public string Address { get; set; }

        /// <summary>
        /// 经度
        /// </summary>
        [Required(ErrorMessage = "经度必填")]
        public decimal Lng { get; set; }

        /// <summary>
        /// 纬度
        /// </summary>
        [Required(ErrorMessage = "纬度必填")]
        public decimal Lat { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        [Required(ErrorMessage = "开始日期必填")]
        public DateTimeOffset StartDate { get; set; }

        /// <summary>
        /// 开始时间: 08:00
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "开始时间: 08:00必填")]
        public string StartTime { get; set; }

        /// <summary>
        /// 结束时间：18:00
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "结束时间：18:00必填")]
        public string EndTime { get; set; }

        /// <summary>
        /// 参赛人数
        /// </summary>
        [Required(ErrorMessage = "参赛人数必填")]
        public int LimitCount { get; set; }

        /// <summary>
        /// 金额
        /// </summary>
        [Required(ErrorMessage = "金额必填")]
        public decimal Amount { get; set; }

        /// <summary>
        /// 活动图片URl
        /// </summary>
        public string? ImgUrl { get; set; }

        /// <summary>
        /// 说明
        /// </summary>
        public string? Remark { get; set; }

        /// <summary>
        /// 组织者
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "组织者必填")]
        public string Host { get; set; }

        /// <summary>
        /// 组织者电话
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "组织者电话必填")]
        public string Phone { get; set; }

        /// <summary>
        /// 组织者logo
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "组织者logo必填")]
        public string Logo { get; set; }

        /// <summary>
        /// validate
        /// </summary>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Amount <= 0)
            {
                yield return new ValidationResult("活动金额小于等于0");
                yield break;
            }

            if (LimitCount <= 0)
            {
                yield return new ValidationResult("活动人数小于等于0");
                yield break;
            }

            if (!TimeSpan.TryParse(StartTime, out var start))
            {
                yield return new ValidationResult("活动开始时间格式错误：ex 08:00");
                yield break;
            }

            if (!TimeSpan.TryParse(EndTime, out var end))
            {
                yield return new ValidationResult("活动结束时间格式错误：ex 18:00");
                yield break;
            }

            if (start >= end)
            {
                yield return new ValidationResult("活动开始时间大于等于结束时间");
                yield break;
            }

            if (ProvinceId.HasValue)
            {
                if (string.IsNullOrWhiteSpace(ProvinceName))
                {
                    yield return new ValidationResult("省份名称为空");
                    yield break;
                }

                if (CityId.HasValue && string.IsNullOrWhiteSpace(CityName))
                {
                    yield return new ValidationResult("城市名称为空");
                    yield break;
                }

                if (!CityId.HasValue)
                {
                    CityName = null;
                }
            }
            else
            {
                ProvinceName = null;
                CityId = null;
                CityName = null;
            }
        }
    }

    /// <summary>
    /// 修改状态dto
    /// </summary>
    public class ActivityStateUpdateDto : IValidatableObject
    {
        /// <summary>
        /// 编号
        /// </summary>
        [Required]
        public Guid ID { get; set; }

        /// <summary>
        /// 活动状态：1报名中、3进行中、4已结束、9已取消
        /// </summary>
        [Required]
        public GameStateEnum State { get; set; }

        /// <summary>
        /// validate
        /// </summary>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (State == GameStateEnum.Joining)
            {
                yield return new ValidationResult("活动状态不正确");
                yield break;
            }
        }
    }
}
